package com.swipemaster.dialogs;

import android.app.Dialog;
import android.graphics.Color;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.swipemaster.R;
import com.swipemaster.activities.GameActivity;
import com.swipemaster.common.Const;
import com.swipemaster.common.Global;
import com.swipemaster.game.GameSurface;
import com.swipemaster.game.modes.ChallengeMode;
import com.swipemaster.levels.SurvivalLevel;
import com.swipemaster.network.task.ShowRecordsTask;
import com.swipemaster.record.Record;
import com.swipemaster.record.RecordsManager;

public class GameDialog extends Dialog
{
	final GameActivity gameActivity;
	final TextView title;
	final ImageView image;
	final TextView newRecord;
	final TextView timesPlayed;
	final TextView timesPlayedString;
	final TextView yourRecord;
	final TextView yourScoreString;
	final TextView yourScore;
	final Button worldRecordBtn;
	final View timesLayout;
	final TextView timeToWinString;
	final TextView timeToWin;
	final TextView timeFor1Star;
	final TextView timeFor2Stars;
	final TextView timeFor3Stars;
	final Button startButton;
	final Button nextButton;

	public GameDialog(final GameActivity activity)
	{
		super(activity);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.game_dialog);

		// Removes border of the dialog.
		getWindow().setBackgroundDrawableResource(android.R.color.transparent);
		getWindow().getAttributes().windowAnimations = R.style.DialogAnimationScale;

		// User can't dismiss the dialog by clicking outside.
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);

		gameActivity = activity;
		title = (TextView) findViewById(R.id.game_dialog_title);
		image = (ImageView) findViewById(R.id.game_dialog_image);
		newRecord = (TextView) findViewById(R.id.game_dialog_new_record);
		timesPlayed = (TextView) findViewById(R.id.game_dialog_times_played);
		timesPlayedString = (TextView) findViewById(R.id.game_dialog_times_played_string);
		yourRecord = (TextView) findViewById(R.id.game_dialog_your_record);
		yourScoreString = (TextView) findViewById(R.id.game_dialog_your_score_string);
		yourScore = (TextView) findViewById(R.id.game_dialog_your_score);
		worldRecordBtn = (Button) findViewById(R.id.game_dialog_world_records_btn);
		timesLayout = findViewById(R.id.game_dialog_times_layout);
		timeToWinString = (TextView) findViewById(R.id.game_dialog_time_to_win_string);
		timeToWin = (TextView) findViewById(R.id.game_dialog_time_to_win);
		timeFor1Star = (TextView) findViewById(R.id.game_dialog_time_for_1_star);
		timeFor2Stars = (TextView) findViewById(R.id.game_dialog_time_for_2_stars);
		timeFor3Stars = (TextView) findViewById(R.id.game_dialog_time_for_3_stars);

		worldRecordBtn.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				new ShowRecordsTask(activity, Global.app.gameMode.recordId).execute();
			}
		});

		final Button backButton = (Button) findViewById(R.id.game_dialog_back_btn);
		backButton.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				onBackPressed();
			}
		});

		startButton = (Button) findViewById(R.id.game_dialog_start_btn);
		startButton.setText(R.string.start);
		startButton.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Global.app.gameSurface.setRunning(true);
				Global.app.isDuringGameplay = true;
				dismiss();
			}
		});

		nextButton = (Button) findViewById(R.id.game_dialog_next_btn);
		nextButton.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				dismiss();
				Global.app.gameMode.onNext();
				activity.onGameCreate();
			}
		});
	}

	@Override
	public void onBackPressed()
	{
		Global.app.musicPlayer.continueMusic = false;
		Global.app.gameSurface.setRunning(false);
		dismiss();
		gameActivity.finish();
		gameActivity.overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
	}

	@Override
	public void show()
	{
		title.setText(Global.app.gameMode.toString());

		final Record record = RecordsManager.getRecord(Global.app.gameMode.recordId);
		if (!record.isValid())
		{
			yourRecord.setText(Global.app.getString(R.string.no_record));
		}
		else
		{
			yourRecord.setText(record.toString());
		}

		if (GameSurface.level instanceof SurvivalLevel)
		{
			timeToWinString.setVisibility(View.VISIBLE);
			timeToWin.setVisibility(View.VISIBLE);
			timeToWin.setText(">" + GameSurface.level.timeToWin + "s");
			timeFor1Star.setText(">" + GameSurface.level.timeFor1Star + "s");
			timeFor2Stars.setText(">" + GameSurface.level.timeFor2Stars + "s");
			timeFor3Stars.setText(">" + GameSurface.level.timeFor3Stars + "s");
		}
		else
		{
			timeFor1Star.setText("<" + GameSurface.level.timeFor1Star + "s");
			timeFor2Stars.setText("<" + GameSurface.level.timeFor2Stars + "s");
			timeFor3Stars.setText("<" + GameSurface.level.timeFor3Stars + "s");
		}

		Dialogs.ensureSize(findViewById(R.id.game_dialog_root_layout));
		super.show();
	}

	public void showOnGameOver(boolean isWinner, boolean isNext, Record record, boolean isNewRecord)
	{
		if (Global.app.gameMode.isTimeConstraint())
		{
			image.setImageResource(Const.getStarsResource(GameSurface.level.getStars()));
		}
		else
		{
			image.setImageResource(0);
		}

		final StringBuilder newRecordText = new StringBuilder();
		if (Global.app.gameMode.isTimeConstraint())
		{
			if (isWinner)
			{
				if (GameSurface.level.getStars() == 0)
				{
					image.setImageResource(R.drawable.win);
				}
				newRecordText.append(Global.app.getString(R.string.win));
			}
			else
			{
				image.setImageResource(0);
				newRecordText.append(Global.app.getString(R.string.lost));
			}
		}

		newRecord.setVisibility(View.VISIBLE);
		if (isNewRecord)
		{
			newRecordText.append(" " + Global.app.getString(R.string.new_record));
		}
		else if (newRecordText.length() == 0)
		{
			newRecord.setVisibility(View.GONE);
		}
		newRecord.setText(newRecordText.toString());

		yourScoreString.setVisibility(View.VISIBLE);
		yourScoreString.setText(Global.app.gameMode.getYourScoreText());
		yourScore.setVisibility(View.VISIBLE);
		yourScore.setText(record.toString());
		yourScore.setShadowLayer(Global.app.consts.dpToPx(Const.scoreShadowRadiousDp),
				Global.app.consts.dpToPx(Const.scoreShadowDxDp), Global.app.consts.dpToPx(Const.scoreShadowDxDp),
				Color.parseColor(Const.scoreShadowColorString));

		if (Global.app.gameMode.isTimeConstraint())
		{
			timesLayout.setVisibility(View.VISIBLE);
		}

		if (Global.app.gameMode instanceof ChallengeMode)
		{
			startButton.setVisibility(View.GONE);
		}
		else
		{
			startButton.setVisibility(View.VISIBLE);
			startButton.setText(Global.app.getString(R.string.restart));
		}

		if (isNext)
		{
			nextButton.setVisibility(View.VISIBLE);
		}

		show();
	}

	public void showOnGameStart()
	{
		newRecord.setVisibility(View.GONE);
		yourScoreString.setVisibility(View.GONE);
		yourScore.setVisibility(View.GONE);

		timesPlayedString.setVisibility(View.VISIBLE);
		timesPlayed.setVisibility(View.VISIBLE);
		timesPlayed.setText(Global.app.gameMode.getPlayedTimes() + "");

		image.setImageResource(0);

		if (Global.app.gameMode.isTimeConstraint())
		{
			timesLayout.setVisibility(View.VISIBLE);
		}

		startButton.setVisibility(View.VISIBLE);
		if (Global.app.isDuringGameplay)
		{
			startButton.setText(Global.app.getString(R.string.resume));
		}
		else
		{
			startButton.setText(Global.app.getString(R.string.start));
		}

		nextButton.setVisibility(View.GONE);

		show();

		nextButton.post(new Runnable()
		{
			@Override
			public void run()
			{
				Global.app.gameMode.showHelp(gameActivity);
			}
		});
	}
}
